//
//  SkillRollerController.m
//  Skill Roller
//
//  Created by John Bestoso on 9/2/08.
//
//  This file is part of Skill Roller.
//
//  Skill Roller is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either selectedVersion 3 of the License, or
//  (at your option) any later selectedVersion.
//
//  Skill Roller is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Skill Roller.  If not, see <http://www.gnu.org/licenses/>.
//

#import "SkillRollerController.h"


@implementation SkillRollerController

@synthesize skills;
@synthesize selectedSkill;

@synthesize roll0;
@synthesize roll1;
@synthesize roll2;
@synthesize roll3;
@synthesize roll4;
@synthesize roll5;
@synthesize roll6;
@synthesize roll7;

@synthesize res0;
@synthesize res1;
@synthesize res2;
@synthesize res3;
@synthesize res4;
@synthesize res5;
@synthesize res6;
@synthesize res7;

@synthesize name0;
@synthesize name1;
@synthesize name2;
@synthesize name3;
@synthesize name4;
@synthesize name5;
@synthesize name6;
@synthesize name7;

- (void)awakeFromNib
{
	
	// Create the skills
	Skill * perception = [[Skill alloc] init];
	[perception setName:@"Perception"];
	
	Skill * senseMotive = [[Skill alloc] init];
	[senseMotive setName:@"Sense Motive"];
	
	[self setSkills:
	[[NSArray arrayWithObjects:perception, senseMotive, nil] retain]];
	[perception release];
	[senseMotive release];
	
	[self setSelectedSkill:[skills objectAtIndex:0]];
	
	// Create the rolls
	[self setRoll0:0];
	[self setRoll1:0];
	[self setRoll2:0];
	[self setRoll3:0];
	[self setRoll4:0];
	[self setRoll5:0];
	[self setRoll6:0];
	[self setRoll7:0];
	
	// Set the results
	[self setRes0:0];
	[self setRes1:0];
	[self setRes2:0];
	[self setRes3:0];
	[self setRes4:0];
	[self setRes5:0];
	[self setRes6:0];
	[self setRes7:0];
	
	// Set the names
	[self setName0:@""];
	[self setName1:@""];
	[self setName2:@""];
	[self setName3:@""];
	[self setName4:@""];
	[self setName5:@""];
	[self setName6:@""];
	[self setName7:@""];
	
}

- (void)dealloc
{
	[skills release];
	[name0 release];
	[name1 release];
	[name2 release];
	[name3 release];
	[name4 release];
	[name5 release];
	[name6 release];
	[name7 release];
	[super dealloc];  
}

- (void)rollSkill:(int)index
{
	
	// Roll the d20
	int roll = rand() % 20 + 1;
	
	// Do the proper assignment. Forced into this by stupid array stuff
	switch (index) {
		case 0:
			[self setRoll0:roll];
			[self setRes0:(roll + [selectedSkill mod0])];
			break;

		case 1:
			[self setRoll1:roll];
			[self setRes1:roll + [selectedSkill mod1]];
			break;
		
		case 2:
			[self setRoll2:roll];
			[self setRes2:roll + [selectedSkill mod2]];
			break;
		
		case 3:
			[self setRoll3:roll];
			[self setRes3:roll + [selectedSkill mod3]];
			break;
			
		case 4:
			[self setRoll4:roll];
			[self setRes4:roll + [selectedSkill mod4]];
			break;
			
		case 5:
			[self setRoll5:roll];
			[self setRes5:roll + [selectedSkill mod5]];
			break;
			
		case 6:
			[self setRoll6:roll];
			[self setRes6:roll + [selectedSkill mod6]];
			break;
			
		case 7:
			[self setRoll7:roll];
			[self setRes7:roll + [selectedSkill mod7]];
			break;
			
		default:
			break;
	}

}

- (void)rollAll
{
	
	// Stupidly roll everything without a loop. Stupid arrays and their hatred
	// of cocoa bindings
	if(name0 != @"")
		[self rollSkill:0];
	if(name1 != @"")
		[self rollSkill:1];
	if(name2 != @"")
		[self rollSkill:2];
	if(name3 != @"")
		[self rollSkill:3];
	if(name4 != @"")
		[self rollSkill:4];
	if(name5 != @"")
		[self rollSkill:5];
	if(name6 != @"")
		[self rollSkill:6];
	if(name7 != @"")
		[self rollSkill:7];
	
	
}

// Stupid IBActions
- (IBAction)rollSkill0:(id)sender
{
	[self rollSkill:0];
}

- (IBAction)rollSkill1:(id)sender
{
	[self rollSkill:1];
}

- (IBAction)rollSkill2:(id)sender
{
	[self rollSkill:2];
}

- (IBAction)rollSkill3:(id)sender
{
	[self rollSkill:3];
}

- (IBAction)rollSkill4:(id)sender
{
	[self rollSkill:4];
}

- (IBAction)rollSkill5:(id)sender
{
	[self rollSkill:5];
}

- (IBAction)rollSkill6:(id)sender
{
	[self rollSkill:6];
}

- (IBAction)rollSkill7:(id)sender
{
	[self rollSkill:7];
}
@end
